<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Banner extends Model
{
    //
    use SoftDeletes;
    protected $table = 'banner';

    public function one($id){
    	return $this->where("id", $id)->get()->toArray();
    }

    public function users(){
    	return $this->belongsTo("App\User", "opertion_user_id", "id");	
    }


    public function listinfo($where,$likeName=[]){
    	if(!empty($likeName)){
    		foreach($likeName as $k => $name){
	    		$res = $this->where($k,'like','%'.$name.'%');
	    	}
	    	return $res->where($where)->paginate(5);
    	}else{
    		return $this->where($where)->paginate(5);
    	}
    }

    public function deleteone($id){
    	return $this->where(array('id'=>$id))->delete();
    }

    public function insert($request){
    	
    	if($request->input("banner_url")){
    		$this->banner_url = $request->input("banner_url");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请上传图片'));
    	}
    	if($request->input("title")){
    		$this->title = $request->input("title");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请填写banner标题'));
    	}

    	if($request->input("link")){
    		$this->link = $request->input("link");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请填写跳转地址'));
    	}
    	if($request->input("order_by")){
    		$this->order_by = $request->input("order_by");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请填写排序值'));
    	}
    	if($request->input("type")){
    		$this->type = $request->input("type");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请选择类别'));
    	}
    	if($request->input("keyword")){
    		$this->keyword = $request->input("keyword");
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'请填写关键字'));
    	}
    	if($request->input("description")){
    		$this->description = $request->input("description");
    	}
    	$this->state = $request->input("state");
    	$this->opertion_user_id = $request->user()->id;
    	if($this->save()){
    		return json_encode(array("code"=>1, 'msg'=>'添加成功'));
        }
    }

    public function updateone($request){
    	if($request->input("id")){
    		$one      = self::find($request->input("id"));
    		if($request->input("banner_url")){
    		$one->banner_url = $request->input("banner_url");
	    	}else{
	    		return json_encode(array("code"=>0, 'msg'=>'请上传图片'));
	    	}
	    	if($request->input("title")){
	    		$one->title = $request->input("title");
	    	}else{
	    		return json_encode(array("code"=>0, 'msg'=>'请填写banner标题'));
	    	}

	    	if($request->input("link")){
	    		$one->link = $request->input("link");
	    	}else{
	    		return json_encode(array("code"=>0, 'msg'=>'请填写跳转地址'));
	    	}
	    	if($request->input("order_by")){
	    		$one->order_by = $request->input("order_by");
	    	}else{
	    		return json_encode(array("code"=>0, 'msg'=>'请填写排序值'));
	    	}
	    	if($request->input("type")){
	    		$one->type = $request->input("type");
	    	}else{
	    		return json_encode(array("code"=>0, 'msg'=>'请选择类别'));
	    	}
	    	if($request->input("keyword")){
	    		$one->keyword = $request->input("keyword");
	    	}else{
	    		return json_encode(array("code"=>0, 'msg'=>'请填写关键字'));
	    	}
	    	if($request->input("description")){
	    		$one->description = $request->input("description");
	    	}
	    	$one->state = $request->input("state");
	    	$one->opertion_user_id = $request->user()->id;
	    	if($one->save()){
	    		return json_encode(array("code"=>1, 'msg'=>'添加成功'));
	        }
    	}else{
    		return json_encode(array("code"=>0, 'msg'=>'参数错误'));
    	}
    }
}
